﻿#region << 文件说明 >>

/*----------------------------------------------------------------
 * 命名空间：test.ViewModel
 * 唯一标识：a7a648e7-efc2-4e53-a48f-56e08a0fdff8
 * 文件名：WebApiController
 *
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2023/9/28 10:16:01
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/

#endregion << 文件说明 >>

using System;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Vampirewal.Core.WebApi;

namespace test.ViewModel;

/// <summary>
///
/// </summary>
[VampirewalController(Vampirewal.Core.Attributes.RegisterType.Singleton, ControllerName = "测试webapi", Description = "哈哈")]
public partial class WebApiController
{
    /// <summary>
    /// 构造函数
    /// </summary>
    public WebApiController()
    {
    }

    ///// <summary>
    ///// 测试
    ///// </summary>
    ///// <param name="str"></param>
    ///// <returns></returns>
    //[VampirewalHttp("/webapi/getstr", "GET")]
    //public  Task GetStr(Request req, Response resp)
    //{
    //    return  resp.WriteJson($"haha");

    //}

    [VampirewalHttp("api/webapi/getstr", HttpMethod.POST, Description = "哈哈POST", MethodName = "测试哈哈1")]
    [TestAction1]
    [TestAction2]
    public string GetStr([VampirewalParameter("ceshi")] TestDTO dto)
    {
        throw new VampirewalWebApiException("测试错误", 404);

        WebApiError.Out("测试错误", 404);
        return $"Name:{dto.Name}";
    }

    [VampirewalHttp("api/webapi/getstr2", HttpMethod.GET, Description = "哈哈GET", MethodName = "测试哈哈2")]
    public string GetStr2([VampirewalParameter("姓名")] string Name, [VampirewalParameter("年龄")] int Age)
    {
        WebApiError.Out("测试错误", 404);

        return $"Name:{Name},Age:{Age}";
    }
}

[VampirewalController(Vampirewal.Core.Attributes.RegisterType.Singleton, ControllerName = "测试webapi2", Description = "哈哈")]
public class TestWebApiController
{
    [VampirewalHttp("api/webapi/getstr3", HttpMethod.GET, Description = "哈哈GET", MethodName = "测试哈哈3")]
    public string GetStr3([VampirewalParameter("姓名")] string Name, [VampirewalParameter("年龄")] int Age)
    {
        WebApiError.Out("测试错误", 404);

        return $"Name:{Name},Age:{Age}";
    }

    [VampirewalHttp("api/webapi/getstr4", HttpMethod.GET, Description = "哈哈GET", MethodName = "测试哈哈4")]
    public string GetStr4([VampirewalParameter("姓名")] string Name, [VampirewalParameter("年龄")] int Age)
    {
        WebApiError.Out("测试错误", 404);

        return $"Name:{Name},Age:{Age}";
    }

    [VampirewalHttp("api/webapi/getstr5", HttpMethod.GET, Description = "哈哈GET", MethodName = "测试哈哈5")]
    public string GetStr5([VampirewalParameter("姓名")] string Name, [VampirewalParameter("年龄")] int Age)
    {
        WebApiError.Out("测试错误", 404);

        return $"Name:{Name},Age:{Age}";
    }

    [VampirewalHttp("api/webapi/getstr6", HttpMethod.GET, Description = "哈哈GET", MethodName = "测试哈哈6")]
    public string GetStr6([VampirewalParameter("姓名")] string Name, [VampirewalParameter("年龄")] int Age)
    {
        WebApiError.Out("测试错误", 404);

        return $"Name:{Name},Age:{Age}";
    }

    [VampirewalHttp("api/webapi/getstr7", HttpMethod.GET, Description = "哈哈GET", MethodName = "测试哈哈7")]
    public string GetStr7([VampirewalParameter("姓名")] string Name, [VampirewalParameter("年龄")] int Age)
    {
        WebApiError.Out("测试错误", 404);

        return $"Name:{Name},Age:{Age}";
    }
}

public class TestDTO
{
    public string Name { get; set; }
}

public class GlobalExceptionMiddleware : IVampirewalMiddleware
{
    private readonly IVampirewalApiResultProvider _ApiResult;

    public GlobalExceptionMiddleware(IVampirewalApiResultProvider ApiResult)
    {
        _ApiResult = ApiResult;
    }

    public async Task InvokeAsync(HttpListenerContext context, Func<Task> next)
    {
        try
        {
            await next.Invoke();
        }
        catch (Exception ex)
        {
            context.Response.StatusCode = 500;
            var apiresult = await _ApiResult.OnResponseStatusCodes(500, errorInfo: ex.Message);
            await context.Response.OutputStream.WriteAsync(Encoding.UTF8.GetBytes(apiresult));
        }
    }
}

public class TestMiddleware : IVampirewalMiddleware
{
    public async Task InvokeAsync(HttpListenerContext context, Func<Task> next)
    {
        //throw new Exception("中间件出错了");

        await next.Invoke();
    }
}

public class TestAction1Attribute : Attribute, IVampirewalActionFilter
{
    public async Task<bool> ExcuteActionAsync(IVampirewalActionFilterContext context, Func<Task<bool>> next)
    {
        return await next.Invoke();
    }
}

public class TestAction2Attribute : VampirewalActionFilterAttribute
{
    public override async Task<bool> ExcuteActionAsync(IVampirewalActionFilterContext context, Func<Task<bool>> next)
    {
        return await next.Invoke();
        //throw new Exception("错误拉");

        //return await Task.FromResult(false);
    }
}